export default class NativeFunction {

  static __init__ = (() => {
    window.AndroidNativeCallback = {};
    window.RustNativeCallback = {};
    window.IosNativeCallback = {};
    if (window.NativePlatform === 'Rust') {
      window.RustNativeJSInterface = {
        updateAppVersionByUrl: (url: string) => window.__TAURI__.core.invoke('update_app_version_by_url', { url }),
        connectSSH: (sessionId: string, host: string, port: number, username: string, password: string) =>
          window.__TAURI__.core.invoke('connect_ssh', { sessionId, host, port, username, password }),

        connectSSHWithPrivateKey: (sessionId: string, host: string, port: number, username: string, privateKey: string) =>
          window.__TAURI__.core.invoke('connect_ssh_with_private_key', { sessionId, host, port, username, privateKey }),

        executeSSHCommand: (sessionId: string, command: string) => window.__TAURI__.core.invoke('execute_ssh_command', {
          sessionId,
          command
        }),

        connectSSHSession: (sessionId: string, host: string, port: number, username: string, password: string) =>
          window.__TAURI__.core.invoke('connect_ssh_session', { sessionId, host, port, username, password }),

        connectSSHSessionWithPrivateKey: (sessionId: string, host: string, port: number, username: string, privateKey: string) =>
          window.__TAURI__.core.invoke('connect_ssh_session_with_private_key', {
            sessionId,
            host,
            port,
            username,
            privateKey
          }),

        executeSSHSessionCommand: (sessionId: string, command: string) => window.__TAURI__.core.invoke('execute_ssh_session_command', {
          sessionId,
          command
        }),

        disconnectSSH: (sessionId: string) => window.__TAURI__.core.invoke('disconnect_ssh', { sessionId }),

        disconnectSSHSession: (sessionId: string) => window.__TAURI__.core.invoke('disconnect_ssh_session', { sessionId })
      };
    }
    return '';
  })();

  static setNativePlatform(platform: 'Android' | 'iOS' | 'Rust'): void {
    window.NativePlatform = platform;
  }

  static async updateAppVersionByUrl(url: string): Promise<boolean> {

    if (window.NativePlatform === 'Android') {
      return await window.AndroidNativeJSInterface.updateAppVersionByUrl(url);
    }
    if (window.NativePlatform === 'iOS') {
      return await window.IosNativeJSInterface.updateAppVersionByUrl(url);
    }
    if (window.NativePlatform === 'Rust') {
      return await window.RustNativeJSInterface.updateAppVersionByUrl(url);
    }
    throw ('NativePlatform is not set');
  }

  /**
   * 连接SSH channel 模式
   * @param sessionId
   * @param host
   * @param port
   * @param username
   * @param password
   */
  static async connectSSH(sessionId: string, host: string, port: number, username: string, password: string, callback: (data: {
    sessionId: string,
    output: string
  }) => string): Promise<boolean> {
    if (window.NativePlatform === 'Android') {
      if (!window.AndroidNativeCallback) {
        window.AndroidNativeCallback = {};
      }
      window.AndroidNativeCallback[sessionId] = callback;
      return await window.AndroidNativeJSInterface.connectSSH(sessionId, host, port, username, password);
    }
    if (window.NativePlatform === 'iOS') {
      if (!window.IosNativeCallback) {
        window.IosNativeCallback = {};
      }
      window.IosNativeCallback[sessionId] = callback;
      return await window.IosNativeJSInterface.connectSSH(sessionId, host, port, username, password);
    }
    if (window.NativePlatform === 'Rust') {
      if (!window.RustNativeCallback) {
        window.RustNativeCallback = {};
      }
      window.RustNativeCallback[sessionId] = callback;
      return await window.RustNativeJSInterface.connectSSH(sessionId, host, port, username, password);
    }
    throw ('NativePlatform is not set');
  }

  /**
   * 连接SSH channel 模式
   * @param sessionId
   * @param host
   * @param port
   * @param username
   * @param privateKey
   */
  static async connectSSHWithPrivateKey(sessionId: string, host: string, port: number, username: string, privateKey: string): Promise<boolean> {

    if (window.NativePlatform === 'Android') {
      return await window.AndroidNativeJSInterface.connectSSHWithPrivateKey(sessionId, host, port, username, privateKey);
    }
    if (window.NativePlatform === 'iOS') {
      return await window.IosNativeJSInterface.connectSSHWithPrivateKey(sessionId, host, port, username, privateKey);
    }
    if (window.NativePlatform === 'Rust') {
      return await window.RustNativeJSInterface.connectSSHWithPrivateKey(sessionId, host, port, username, privateKey);
    }
    throw ('NativePlatform is not set');
  }

  /**
   * 执行SSH channel 模式
   * @param sessionId
   * @param command
   * @param callback
   */
  static async executeSSHCommand(sessionId: string, command: string, callback: (data: {
    sessionId: string,
    output: string
  }) => string): Promise<string> {

    if (window.NativePlatform === 'Android') {
      if (!window.AndroidNativeCallback) {
        window.AndroidNativeCallback = {};
      }
      window.AndroidNativeCallback[sessionId] = callback;
      return await window.AndroidNativeJSInterface.executeSSHCommand(sessionId, command);
    }
    if (window.NativePlatform === 'iOS') {
      if (!window.IosNativeCallback) {
        window.IosNativeCallback = {};
      }
      window.IosNativeCallback[sessionId] = callback;
      return await window.IosNativeJSInterface.executeSSHCommand(sessionId, command);
    }
    if (window.NativePlatform === 'Rust') {
      if (!window.RustNativeCallback) {
        window.RustNativeCallback = {};
      }
      window.RustNativeCallback[sessionId] = callback;
      return await window.RustNativeJSInterface.executeSSHCommand(sessionId, command);
    }
    throw ('NativePlatform is not set');
  }

  /**
   * 连接SSH 普通模式
   * @param sessionId
   * @param host
   * @param port
   * @param username
   * @param password
   */
  static async connectSSHSession(sessionId: string, host: string, port: number, username: string, password: string): Promise<boolean> {
    if (window.NativePlatform === 'Android') {
      return await window.AndroidNativeJSInterface.connectSSHSession(sessionId, host, port, username, password);
    }
    if (window.NativePlatform === 'iOS') {
      return await window.IosNativeJSInterface.connectSSHSession(sessionId, host, port, username, password);
    }
    if (window.NativePlatform === 'Rust') {
      return await window.RustNativeJSInterface.connectSSHSession(sessionId, host, port, username, password);
    }
    throw ('NativePlatform is not set');
  }

  /**
   * 连接SSH 普通模式
   * @param sessionId
   * @param host
   * @param port
   * @param username
   * @param privateKey
   */
  static async connectSSHSessionWithPrivateKey(sessionId: string, host: string, port: number, username: string, privateKey: string): Promise<boolean> {
    if (window.NativePlatform === 'Android') {
      return await window.AndroidNativeJSInterface.connectSSHSessionWithPrivateKey(sessionId, host, port, username, privateKey);
    }
    if (window.NativePlatform === 'iOS') {
      return await window.IosNativeJSInterface.connectSSHSessionWithPrivateKey(sessionId, host, port, username, privateKey);
    }
    if (window.NativePlatform === 'Rust') {
      return await window.RustNativeJSInterface.connectSSHSessionWithPrivateKey(sessionId, host, port, username, privateKey);
    }
    throw ('NativePlatform is not set');
  }

  /**
   * 执行SSH  普通模式
   * @param sessionId
   * @param command
   */
  static async executeSSHSessionCommand(sessionId: string, command: string): Promise<string> {
    if (window.NativePlatform === 'Android') {
      return await window.AndroidNativeJSInterface.executeSSHSessionCommand(sessionId, command);
    }
    if (window.NativePlatform === 'iOS') {
      return await window.IosNativeJSInterface.executeSSHSessionCommand(sessionId, command);
    }
    if (window.NativePlatform === 'Rust') {
      return await window.RustNativeJSInterface.executeSSHSessionCommand(sessionId, command);
    }
    throw ('NativePlatform is not set');
  }

  static async disconnectSSH(sessionId: string): Promise<boolean> {
    if (window.NativePlatform === 'Android') {
      return await window.AndroidNativeJSInterface.disconnectSSH(sessionId);
    }
    if (window.NativePlatform === 'iOS') {
      return await window.IosNativeJSInterface.disconnectSSH(sessionId);
    }
    if (window.NativePlatform === 'Rust') {
      return await window.RustNativeJSInterface.disconnectSSH(sessionId);
    }
    throw ('NativePlatform is not set');
  }

  static async disconnectSSHSession(sessionId: string): Promise<boolean> {
    if (window.NativePlatform === 'Android') {
      return await window.AndroidNativeJSInterface.disconnectSSHSession(sessionId);
    }
    if (window.NativePlatform === 'iOS') {
      return await window.IosNativeJSInterface.disconnectSSHSession(sessionId);
    }
    if (window.NativePlatform === 'Rust') {
      return await window.RustNativeJSInterface.disconnectSSHSession(sessionId);
    }
    throw ('NativePlatform is not set');
  }

  static async reloadWeb(): Promise<boolean> {
    if (window.NativePlatform === 'Android') {
      return await window.AndroidNativeJSInterface.reload();
    }
    if (window.NativePlatform === 'iOS') {
      return await window.IosNativeJSInterface.reload();
    }
    if (window.NativePlatform === 'Rust') {
      return await window.RustNativeJSInterface.reload();
    }
    throw ('NativePlatform is not set');
  }

}
